<?php
class ClubFavorite
{
    /**
     * Parent instance
     */
    private static $_modelParent    = null;

    /**
     * Instance
     */
    protected static $_instance     = null;

    /**
     * Constructor of class
     */
    protected function  __construct()
    {
        //Init parent model
        $options    = array(
                'adapter'   => 'mysql'
        );
        $this->_modelParent = Fpt_Business_ClubFavorite::getInstance($options);
    }

    /**
     * Get singleton instance
     */
    public final static function getInstance()
    {
        //Check instance
        if ( is_null(self::$_instance) )
        {
            self::$_instance    = new self();
        }//end if

        //Return instance
        return self::$_instance;
    }

    /**
     * Get list my post
     */
    public function getListMyPost($userId, $limit, $offset)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $param  = array($userId);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_MY_POST, $param);
        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);
        
        //Check memcache
        if ( $result === false )
        {
            $result     = '-1';
            $resultDB = $this->_modelParent->getListMyPost($userId);
            if ( !empty($resultDB) && is_array($resultDB) )
            {
                $result = array();
                foreach ( $resultDB as $row )
                {
                    $result[]   = $row['articleid'];
                }//end if
            }//end if

            //Set memcache
            $memcacheInstance->write($memcacheKey, $result);
        }//end if

        //Proccess data
        $return = array('list'=>null, 'total'=>0);
        if ( !empty($result) && is_array($result) )
        {
            $return['total']    = count($result);
            $list   = array_slice($result, $offset, $limit);
            $return['list']     = $this->getListArticleDetail($list, 1, true);
        }//end if

        //Return
        return $return;
    }

    /**
     * Get article by category and status
     */
    public function getArticleByCategoryAndStatus($category, $status, $limit, $offset)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $param  = array($category, $status);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);
        
        //Check memcache
        if ( $result === false )
        {
            $result     = '-1';
            $resultDB   = $this->_modelParent->getArticleByCategoryAndStatus($category, $status);
            if ( !empty($resultDB) && is_array($resultDB) )
            {
                $result = array();
                foreach ( $resultDB as $row )
                {
                    $result[]   = $row['articleid'];
                }//end if
            }//end if

            //Set memcache
            $memcacheInstance->write($memcacheKey, $result);
        }//end if
        
        //Proccess data
        $return = array('list'=>null, 'total'=>0);
        if ( !empty($result) && is_array($result) )
        {
            $return['total']    = count($result);
            $list   = array_slice($result, $offset, $limit);
            $return['list']     = $this->getListArticleDetail($list, $status, true);
        }//end if

        //Return
        return $return;
    }

    /**
     * Get article hot
     */
    public function getArticleHot($limit, $offset=0, $getTotal=false)
    {
        //Set max record
        $maxRecord  = 30;

        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT, null);
        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);
        
        //Check memcached
        if ( $result === false )
        {
            //Get list article hot by admin set
            $listAdminSet   = $this->getArticleHotByAdminSet(0, $maxRecord, 0);
            $count          = (!empty($listAdminSet) && is_array($listAdminSet))?count($listAdminSet):0;
            if ( $count < $limit )
            {
                //Get list article hot by view & comment
                $orderBy    = ' a.total_vote DESC, a.total_view DESC ';
                $listViewComment    = $this->getArticleHotByViewComment($orderBy, $maxRecord-$limit, 0);
                $result     = array_slice(array_unique(array_merge((array)$listAdminSet, (array)$listViewComment)), 0, $maxRecord);
            }
            else
            {
                $result = (array)$listAdminSet;
            }//end if
            
            //Set memcached if not null
            if ( !empty($result) && is_array($result) )
            {
                //Get memcache key
                $memcacheKeyExp = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT_EXPIRED, null);
                //Set memcache
                $memcacheInstance->write($memcacheKey, $result, $memcacheKeyExp);
            }//end if
        }//end if

        //Proccess data
        $return = array('list'=>null, 'total'=>0);
        if ( !empty($result) && is_array($result) )
        {
            $return['total']    = count($result);
            $result = array_slice($result, $offset, $limit);
            $return['list'] = $this->getListArticleDetail($result, 1, true);
        }//end if
        
        //Return
        return ($getTotal)?$return:$return['list'];
    }

    /**
     * Get team expression
     */
    public function getTeamExpression($limit)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_TEAM_EXPRESSION, null);
        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);

        //Check memcached
        if ( $result === false )
        {
            //Get list article hot by admin set
            $result = $this->getArticleHotByAdminSet(1, 5, 0); //Max 5 record
            //Set memcache
            $resultMemcached    = (!empty($result) && is_array($result))?$result:'-1';
            $memcacheInstance->write($memcacheKey, $resultMemcached);
        }//end if

        //Proccess data
        $return = null;
        if ( !empty($result) && is_array($result) )
        {
            $result = array_slice($result, 0, $limit);
            $return = $this->getListArticleDetail($result, 1, true);
        }//end if

        //Return
        return $return;
    }

    /**
     * Get article hot by admin set
     */
    public function getArticleHotByAdminSet($category, $limit, $offset)
    {
        $return = array();
        $result = $this->_modelParent->getArticleHotByAdminSet($category, $limit, $offset);
        if ( !empty($result['list']) && is_array($result['list']) )
        {
            foreach ( $result['list'] as $row )
            {
                $return[]   = $row['articleid'];
            }//end foreach
        }//end if

        //Return
        return $return;
    }

    /**
     * Get article hot by view & comment
     */
    public function getArticleHotByViewComment($orderBy, $limit, $offset)
    {
        $return = array();
        $result = $this->_modelParent->getArticleHotByViewComment($orderBy, $limit, $offset);
        if ( !empty($result['list']) && is_array($result['list']) )
        {
            foreach ( $result['list'] as $row )
            {
                $return[]   = $row['articleid'];
            }//end foreach
        }//end if

        //Return
        return $return;
    }

    /**
     * Get list article detail by list article id
     */
    public function getListArticleDetail($arrId, $status, $isGetStatistic=false)
    {
        $return = array();
        foreach ( $arrId as $id )
        {
            $detail = $this->getArticleDetail($id);
            if ( isset($detail['articleid']) && is_numeric($detail['articleid']) && $detail['articleid']>0 && $detail['status']==$status )
            {
                if ( $isGetStatistic )
                {
                    $statistic  = $this->getArticleStatistic($id, true);
                    $detail['total_comment']    = (isset($statistic['total_comment']) && is_numeric($statistic['total_comment']))?$statistic['total_comment']:0;
                    $detail['total_view']       = (isset($statistic['total_view']) && is_numeric($statistic['total_view']))?$statistic['total_view']:0;
                    $detail['total_vote']       = (isset($statistic['total_vote']) && is_numeric($statistic['total_vote']))?$statistic['total_vote']:0;
                }//end if
                $return[$id]    = $detail;
            }//end if
        }//end foreach

        //Return
        return $return;
    }

    /**
     * Get article detail
     */
    public function getArticleDetail($articleId)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $params         = array($articleId);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_ARTICLE_DETAIL, $params);
        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);
        //echo $memcacheKey; die;
        //Not memcached
        if ( $result === false )
        {
            $result = $this->_modelParent->getArticleDetail($articleId);
            if ( !empty($result) && is_array($result) )
            {
                //Set to memcached
                $memcacheInstance->write($memcacheKey, $result);
            }//end if
        }//end if

        //Return
        return $result;
    }

    /**
     * Update article status
     */
    public function updateArticleStatus($params)
    {
        if ( $this->_modelParent->updateArticleStatus($params['articleid'], $params['status']) )
        {
            //Delete memcached
            try
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Set param
                $param          = array($params['articleid']);
                //Get memcache key - article detail
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_ARTICLE_DETAIL, $param);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);

                //Get memcache key - list article status new
                $param  = array($params['categoryid'], $params['status']);
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);

                //Get memcache key - list article status old
                $param  = array($params['categoryid'], $params['status_old']);
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);

                //List: 0 - bai du thi hot, 1 - doi bong tieu bieu
                if ( $params['categoryid'] == 0 )
                {
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT, null);
                    //Delete memache
                    $memcacheInstance->delete($memcacheKey);
                    //$otherMemcacheInstance->delete($memcacheKey);
                }
                elseif ( $params['categoryid'] == 1 )
                {
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_TEAM_EXPRESSION, null);
                    //Delete memache
                    $memcacheInstance->delete($memcacheKey);
                    //$otherMemcacheInstance->delete($memcacheKey);
                }//end if

                //List my post
                if ( $params['categoryid']==0 && is_numeric($params['userid']) && $params['userid']>0 )
                {
                    //Set param
                    $param  = array($params['userid']);
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_MY_POST, $param);
                    //Delete memache
                    $memcacheInstance->delete($memcacheKey);
                    //$otherMemcacheInstance->delete($memcacheKey);
                }//end if

                //Return
                return true;
            }
            catch ( Exception $e )
            {
                self::logError($e);
                return false;
            }//end try catch
        }
        else
        {
            return false;
        }//end if
    }

    /**
     * Check page view
     */
    public function checkPageView($articleId, $sessionId)
    {
        try
        {
            //Get memcache instance
            $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
            //Get ClubCaching instance
            $clubCachingInstance    = ClubCaching::getInstance();
            //Set param
            $params         = array($articleId, $sessionId);
            //Get memcache key
            $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::PV_CLUB_VALID, $params);
            //Read memache
            $is_viewed      = $memcacheInstance->read($memcacheKey);
            //Return
            return !$is_viewed?true:false;
        }
        catch ( Exception $e )
        {
            self::logError($e);
            return false;
        }//end try catch
    }

    /**
     * Insert page view
     */
    public function insertPageView($params)
    {
        if ( $this->checkPageView($params['articleid'], $params['sessionid']) )
        {
            //Insert
            try
            {
                //Call insert db
                if ( $this->_modelParent->insertPageView($params) )
                {
                    //Get memcache instance
                    $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
                    //Get ClubCaching instance
                    $clubCachingInstance    = ClubCaching::getInstance();
                    //Set param
                    $param          = array($params['articleid'], $params['sessionid']);
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::PV_CLUB_VALID, $param);
                    //Get memcache key expire
                    $memcacheKeyExp = $clubCachingInstance->getObjKeyByType(ClubCaching::PV_CLUB_VALID_EXPIRED, null);
                    //Set memcache
                    $memcacheInstance->write($memcacheKey, $params['sessionid'], $memcacheKeyExp);

                    //Precall get article statistic
                    $this->getArticleStatistic($params['articleid'], false);

                    //Return
                    return true;
                }
                else
                {
                    //Return
                    return false;
                }//end if
            }
            catch ( Exception $e )
            {
                self::logError($e);
                return false;
            }//end try catch
        }//end if

        //Return
        return false;
    }

    /**
     * Get article statistic
     */
    public function getArticleStatistic($articleId, $checkCache=true)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $params         = array($articleId);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_ARTICLE_STATISTIC, $params);

        //Read memache
        $result         = false;
        if ( $checkCache )
        {
            $result     = $memcacheInstance->read($memcacheKey);
        }//end if

        //Not memcached
        if ( $result === false )
        {
            $result = $this->_modelParent->getArticleStatistic($articleId);
            $result = (!empty($result) && is_array($result))?$result:'-1';

            //Set to memcached
            $memcacheInstance->write($memcacheKey, $result);
            if ( !$checkCache )
            {
                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);
                //$otherMemcacheInstance->write($memcacheKey, $result);
            }//end if
        }//end if

        //Return
        return $result;
    }

    /**
     * Insert article
     */
    public function insertArticle($params)
    {
        $articleId  = $this->_modelParent->insertArticle($params);

        //Remove memcache
        if ( $articleId > 0 )
        {
            //Get memcache instance
            $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

            //Other memcache instance
            //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

            //Get ClubCaching instance
            $clubCachingInstance    = ClubCaching::getInstance();
            //Set param
            $param  = array($params['categoryid'], $params['status']);
            //Get memcache key - list article
            $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
            //Delete memache
            $memcacheInstance->delete($memcacheKey);
            //$otherMemcacheInstance->delete($memcacheKey);

            //Get memcache key - list my post
            if ( is_numeric($params['userid']) && $params['userid']>0 && $params['status']==1 )
            {
                //Set param
                $param  = array($params['userid']);
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_MY_POST, $param);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }//end if
        }//end if

        //Return
        return $articleId;
    }

    /**
     * Upload file
     */
    public function uploadFile($articleid, $filename, $uploaded_file, $static_path, $strPath)
    {
        //Set return
        $return = array('thumb'=>null);

        if ( !empty($filename) )
        {
            //Set root path
            $uploaded_file->setDestination($strPath);

            foreach ( $uploaded_file->getFileInfo() as $info )
            {
                if ( !empty($info['name']) )
                {
                    $strPos     = strpos($info['name'], '.', strlen($info['name']) - 4);
                    $strExt     = substr($info['name'], $strPos + 1, 3);
                    $filetype   = $uploaded_file->getMimeType();

                    //Get extension
                    switch ($filetype)
                    {
                        case 'image/jpeg':
                            $strExt = 'jpg';
                            break;
                        case 'image/gif':
                            $strExt = 'gif';
                            break;
                        case 'image/png':
                            $strExt = 'png';
                            break;
                    }//end switch

                    $strFilename    = time() . '.' . $strExt;

                    //Rename file
                    $uploaded_file->addFilter('Rename', array('target'=>$strPath.Fpt_Filter::upperTolower($strFilename), 'overwrite'=>true));

                    if ( !$uploaded_file->receive($info['name']) )
                    {
                        return null;
                    }
                    elseif ( $articleid > 0 )
                    {
                        //Remove file cu neu co
                        $details = $this->getArticleDetail($articleid);

                        if (!empty ($details['image']))
                        {
                            //remove image
                            unlink($static_path.$details['image']);
                        }//end if
                    }//end if

                    $thumb = '/site/'.Fpt_Filter::upperTolower($strFilename);
                }//end if

                //Set return
                $return['thumb']    = $thumb;
            }//end foreach
        }//end if

        //Return
        return $return;
    }

    /**
     * Check user vote
     */
    public function checkUserVote($articleId, $userId)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $params         = array($articleId, $userId);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_CHECK_ARTICLE_USER_VOTE, $params);

        //Read memcache
        $result     = $memcacheInstance->read($memcacheKey);
        if ( $result === false )
        {
            $result = $this->_modelParent->checkUserVote($articleId, $userId);
            $resultMemcached    = (isset($result['vote_id']) && is_numeric($result['vote_id']) && $result['vote_id']>0)?$result:'-1';
            //Set memcached
            $memcacheInstance->write($memcacheKey, $resultMemcached);
        }//end if

        //Return
        return (isset($result['vote_id']) && is_numeric($result['vote_id']) && $result['vote_id']>0)?true:false;
    }

    /**
     * Check user vote by email
     */
    public function checkUserVoteByEmail($articleId, $email)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $params         = array($articleId, $email);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_CHECK_ARTICLE_USER_VOTE, $params);
        //Read memcache
        $result     = $memcacheInstance->read($memcacheKey);

        if ( $result === false )
        {
            $result = $this->_modelParent->checkUserVoteByEmail($articleId, $email);
            $resultMemcached    = (isset($result['vote_id']) && is_numeric($result['vote_id']) && $result['vote_id']>0)?$result:'-1';
            //Set memcached
            $memcacheInstance->write($memcacheKey, $resultMemcached);
        }//end if

        //Return
        return (isset($result['vote_id']) && is_numeric($result['vote_id']) && $result['vote_id']>0)?true:false;
    }

    /**
     * Insert vote
     */
    public function insertVote($params)
    {
        $voteId = $this->_modelParent->insertVote($params);
        //Remove memcache
        if ( $voteId > 0 )
        {
            //Get memcache instance
            $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

            //Other memcache instance
            //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

            //Get ClubCaching instance
            $clubCachingInstance    = ClubCaching::getInstance();

            //Set param to remove check user vote
            $param          = array($params['articleid'], $params['userid']);
            //Get memcache key
            $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_CHECK_ARTICLE_USER_VOTE, $param);

            //Read memcache
            $memcacheInstance->delete($memcacheKey);
            //$otherMemcacheInstance->delete($memcacheKey);

            //Check email
            if ( !empty($params['email']) )
            {
                //Set param to remove check user vote
                $param          = array($params['articleid'], $params['email']);
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_CHECK_ARTICLE_USER_VOTE, $param);

                //Read memcache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }//end if

            //Precall get article statistic
            $this->getArticleStatistic($params['articleid'], false);
        }//end if

        //Return
        return $voteId;
    }

    /**
     * Get list comment
     */
    public function getListComment($articleId, $limit, $offset)
    {
        //Get memcache instance
        $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
        //Get ClubCaching instance
        $clubCachingInstance    = ClubCaching::getInstance();
        //Set param
        $params         = array($articleId);
        //Get memcache key
        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_COMMENT_ARTICLE, $params);

        //Read memache
        $result         = $memcacheInstance->read($memcacheKey);

        //Not memcached
        if ( $result === false )
        {
            $result = $this->_modelParent->getListComment($articleId);
            //Set to memcached
            $memcacheInstance->write($memcacheKey, (!empty($result) && is_array($result)?$result:'-1'));
        }//end if

        //Set return
        $return = array('list'=>null, 'total'=>0);
        if ( !empty($result) && is_array($result) && $limit>0 )
        {
            $result = array_reverse($result);
            $return['total']    = count($result);
            $return['list']     = array_slice($result, $offset, $limit);
        }
        elseif ( !empty($result) && is_array($result) )
        {
            $result = array_reverse($result);
            $return['total']    = count($result);
            $return['list']     = $result;
        }//end if

        //Return
        return $return;
    }

    /**
     * Insert comment
     */
    public function insertComment($params)
    {
        $commentId  = $this->_modelParent->insertComment($params);

        //Remove memcache
        if ( $commentId > 0 )
        {
            //Get memcache instance
            $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

            //Other memcache instance
            //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

            //Get ClubCaching instance
            $clubCachingInstance    = ClubCaching::getInstance();
            //Set param
            $param  = array($params['articleid']);
            //Get memcache key
            $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_COMMENT_ARTICLE, $param);
            //Delete memache
            $memcacheInstance->delete($memcacheKey);
            //$otherMemcacheInstance->delete($memcacheKey);

            //Precall get article statistic
            $this->getArticleStatistic($params['articleid'], false);
        }//end if

        //Return
        return $commentId;
    }

    /**
     * Get euro news api
     */
    public function getEuroNews($limit)
    {
        return null;
    }

    /**
     * Log error
     */
    public static function logError($e)
    {
        if ( APP_ENV == 'development' )
        {
            echo $e->getMessage();
        }
        else
        {
            Fpt_Log::sendlog(4, $e->getCode(), $e->getMessage(), $e->getTraceAsString(), null, null);
        }//end if
    }
    /*
     * get all list category
     * @param <array> $para, <int> $limit, <int> $offset
     * return <array> $result
    */
    public function getArticlesInfo($para,$limit,$offset)
    {
        $result = $this->_modelParent->getArticlesInfo($para,$limit,$offset);
        return $result;
    }
    /*
     * insert data for hot news
     * @param <array> $arrParam
     * return <int> $result
    */
    public function insertHotNews($arrParams)
    {
        $result = $this->_modelParent->insertHotNews($arrParams);
        //Check delete memcached
        if ( $result > 0 )
        {
            //List: 0 - bai du thi hot, 1 - doi bong tieu bieu
            if ( $arrParams['categoryid'] == 0 )
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT, null);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }
            elseif ( $arrParams['categoryid'] == 1 )
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_TEAM_EXPRESSION, null);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }//end if
        }//end if

        //Return
        return $result;
    }

	/**
     * Delete article hot
     */
    public function deleteHotNews($arrParams)
    {
        $result = $this->_modelParent->deleteHotNews($arrParams['articleid']);
        //Check delete memcached
        if ( $result > 0 )
        {
            //List: 0 - bai du thi hot, 1 - doi bong tieu bieu
            if ( $arrParams['categoryid'] == 0 )
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT, null);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }
            elseif ( $arrParams['categoryid'] == 1 )
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_TEAM_EXPRESSION, null);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
            }//end if
        }//end if

        //Return
        return $result;
    }
    /*
     * get list hot news
     * @param : NULL
     * return <array> $result
    */
    public function getListHotNews()
    {
        $result = $this->_modelParent->getListHotNews();
        return $result;
    }
    /*
     * update article
     * @param : <array> $params
     * return <int> $result
    */
    public function updateArticle($params)
    {
        $result  = $this->_modelParent->updateArticle($params);

        //Remove memcache
        if ($result)
        {
            try
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);
                
                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Set param
                $param  = array($params['articleid']);
                //Get memcache key - detail article
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_ARTICLE_DETAIL, $param);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);
                
                //Check status to delete memcached list
                if ( $params['status'] != $params['status_old'] )
                {
                    //Get memcache key - list article status new
                    $param  = array($params['categoryid'], $params['status']);
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
                    //Delete memache
                    $memcacheInstance->delete($memcacheKey);
                    //$otherMemcacheInstance->delete($memcacheKey);

                    //Get memcache key - list article status old
                    $param  = array($params['categoryid'], $params['status_old']);
                    //Get memcache key
                    $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_CATEGORY, $param);
                    //Delete memache
                    $memcacheInstance->delete($memcacheKey);
                    //$otherMemcacheInstance->delete($memcacheKey);

                    //List: 0 - bai du thi hot, 1 - doi bong tieu bieu
                    if ( $params['categoryid'] == 0 )
                    {
                        //Get memcache key
                        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_ARTICLE_HOT, null);
                        //Delete memache
                        $memcacheInstance->delete($memcacheKey);
                        //$otherMemcacheInstance->delete($memcacheKey);
                    }
                    elseif ( $params['categoryid'] == 1 )
                    {
                        //Get memcache key
                        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_TEAM_EXPRESSION, null);
                        //Delete memache
                        $memcacheInstance->delete($memcacheKey);
                        //$otherMemcacheInstance->delete($memcacheKey);
                    }//end if

                    //List my post
                    if ( $params['categoryid']==0 && is_numeric($params['userid']) && $params['userid']>0 )
                    {
                        //Set param
                        $param  = array($params['userid']);
                        //Get memcache key
                        $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_MY_POST, $param);
                        //Delete memache
                        $memcacheInstance->delete($memcacheKey);
                        //$otherMemcacheInstance->delete($memcacheKey);
                    }//end if
                }//end if

                //Return
                return true;
            }
            catch ( Exception $e )
            {
                self::logError($e);
                return false;
            }//end try catch
        }//end if
        //Return
        return false;
    }
    /**
     * delete comment
     */
    public function deleteComment($commentId,$articleid)
    {
        if ( $this->_modelParent->deleteComment($commentId,$articleid) )
        {
            //Remove cache article's info
            try
            {
                //Get memcache instance
                $memcacheInstance       = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, ClubCaching::CURRENT_ZONE_NAME, ClubCaching::IDC);

                //Other memcache instance
                //$otherMemcacheInstance  = Fpt_Global::getMemcacheInstance(ClubCaching::SHARDING_ID, (ClubCaching::CURRENT_ZONE_NAME==ClubCaching::HCM_ZONE_NAME?ClubCaching::HN_ZONE_NAME:ClubCaching::HCM_ZONE_NAME), ClubCaching::IDC);

                //Get ClubCaching instance
                $clubCachingInstance    = ClubCaching::getInstance();
                //Set param
                $params         = array($articleid);
                //Get memcache key
                $memcacheKey    = $clubCachingInstance->getObjKeyByType(ClubCaching::CLUB_LIST_COMMENT_ARTICLE, $params);
                //Delete memache
                $memcacheInstance->delete($memcacheKey);
                //$otherMemcacheInstance->delete($memcacheKey);

                //Precall get article statistic
                $this->getArticleStatistic($articleid, false);

                //Return
                return true;
            }
            catch ( Exception $e )
            {
                self::logError($e);
                return false;
            }//end try catch
        }
        else
        {
            return false;
        }//end if
    }

    /**
     * Get list comment new
     */
    public function getListCommentNew($limit, $offset)
    {
        //Return
        return $this->_modelParent->getListCommentNew($limit, $offset);
    }
}